<?php

class Megadrupal_Slider_Model_Slider extends Mage_Core_Model_Abstract
{
    protected function _construct(){
       $this->_init("slider/slider");
    }
    
    function getMDSlider($slid) {
        // Get slider info
        $slider = Mage::getModel("slider/slider")->load($slid)->getData();
        // parsing slider info to array
        $slider['settings'] = unserialize($slider['settings']);
        // create slideshow data for Slider
        $settings = $slider['settings'];
        $data_properties = array();
        $data_properties[] = 'data-thumb-width="'.$settings['thumb_width'].'"';
        $data_properties[] = 'data-thumb-height="'.$settings['thumb_height'].'"';
        $slider['data_properties'] = implode(' ', $data_properties);
        
        // Get slide of slider above
        $slides  =  Mage::getModel("slider/slide")
                    ->getCollection()
                    ->addFieldToFilter('slid', $slid)->getData();
        
        // parsing slide info to array
        $_countSlides = count($slides);
        if($_countSlides > 0) {
            for ($iSlides = 0; $iSlides < $_countSlides; $iSlides++) {
                $slides[$iSlides]['settings'] = unserialize($slides[$iSlides]['settings']);
                $slides[$iSlides]['layers'] = unserialize($slides[$iSlides]['layers']);
                // create slideshow data for Slide
                $slides[$iSlides]['data_properties'] = array();
                $slides[$iSlides]['data_properties'][] = 'data-timeout="'.$slides[$iSlides]['settings']['timelinewidth'].'00"';
                
                $slides[$iSlides] = $this->prepareSlide($slides[$iSlides]);    
                
                // prepare content for Layer of this slice
                $_countLayer = count($slides[$iSlides]['layer']);
                if($_countLayer > 0) {
                    for ($iLayer = 0; $iLayer < $_countLayer; $iLayer++) {
                        $slides[$iSlides]['layer'][$iLayer] = $this->prepareLayer($slides[$iSlides]['layer'][$iLayer]);
                    }
                }
                
            }
        }        
        
        $_return = array('slider' => $slider, 'slides' => $slides);
        
        return $_return;
    }
    
     public function prepareSlide($slice) { 
        $thumb_url = '';
        $slice['background_style'] = "";
        if ($slice['settings']['background_image'] != -1) {
          # Load background image
//                  $background = $this->file_load($slice['settings']['background_image']);
//                  $create_bg_imgs = $variables['slider_settings']['create_bg_imgstyle'];
//                  if ($create_bg_imgs) {
//                    $slice['background_url'] = image_style_url('md_slider_' . $slide->slid . '_bg', $background->uri);
//                  } else {
//                    $slice['background_url'] = file_create_url($background->uri);
//                  }
//
//                  # Load thumbnail image
//                  if ($slide->settings['custom_thumbnail'] != -1) {
//                    $thumb = file_load($slide->settings['custom_thumbnail']);
//                  }
//                  else {
//                    $thumb = $background;
//                  }
//                  $thumb_url = image_style_url('md_slider_' . $slide->slid . '_thumb', $thumb->uri);
        }
        else {
          $slice['background_style'] = ' style="width: auto; height: auto; max-width: none"';
          $slice['background_url'] = '';
//                  if ($slide->settings['custom_thumbnail'] != -1) {
//                    $thumb = file_load($slide->settings['custom_thumbnail']);
//                    $thumb_url = image_style_url('md_slider_' . $slide->slid . '_thumb', $thumb->uri);
//                  }
//                  else {
//                    $thumb_url = image_style_url('md_slider_' . $slide->slid . '_thumb', "public://md_slider_def_bg.jpg");
//                  }
        }
        $data_properties[] = "data-thumb='$thumb_url'";        
               
        $slice['data_properties'] = implode(' ', $data_properties);
        
        return $slice;
     }
    
    public function prepareLayer($layer) {   
        # Create css inline style for layer
//        $layer['styles'] = process_layer_style((object)$layer);

        # Get image url for image layer
        if ($layer['type'] == 'image') {
          $layer['file_url'] = '';
          if (isset($layer['fileid'])) {
//            $image = file_load($layer['fileid']);
//            $layer['url'] = file_create_url($image->uri);
          }
        }

//        $data = process_element_data($layer);
//        $layer['data'] = $data;
    
        $class = array('md-object');
        if (isset($layer['style']) && $layer['style'] != 'none') {
            $class[] = $layer['style'];
        }
        $layer['class'] = implode(' ', $class);
        
        return $layer;
    }
    
    function file_load($img) {
        return $img;
    }

}
	 